<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>面试评分权重设置</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/boxicons@2.0.7/css/boxicons.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4a90e2;
            --error-color: #ff4d4f;
            --success-color: #52c41a;
            --border-radius: 8px;
            --gradient-purple: linear-gradient(135deg, #9c27b0, #673ab7);
        }

        body {
            background-color: #f5f7fa;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            margin: 0;
            padding: 20px 0;
        }

        .weight-container {
            max-width: 700px;
            margin: 0 auto;
            padding: 20px;
            background: white;
            border-radius: var(--border-radius);
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
        }

        .page-title {
            color: white;
            font-size: 20px;
            font-weight: 600;
            margin-bottom: 15px;
            padding: 12px 20px;
            background: var(--gradient-purple);
            border-radius: var(--border-radius);
            box-shadow: 0 2px 8px rgba(156, 39, 176, 0.2);
        }

        .form-label {
            font-weight: 500;
            color: #4a5568;
            margin-bottom: 4px;
            font-size: 14px;
        }

        .form-control {
            border: 2px solid #e2e8f0;
            border-radius: var(--border-radius);
            padding: 6px 12px;
            height: auto;
            transition: all 0.3s ease;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
        }

        .weight-group {
            position: relative;
            margin-bottom: 12px;
            padding: 12px;
            background: #f8fafc;
            border-radius: var(--border-radius);
            transition: all 0.3s ease;
        }

        .weight-group:hover {
            background: #f1f5f9;
        }

        .weight-icon {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary-color);
            font-size: 18px;
            opacity: 0.5;
        }

        .weight-sum {
            margin: 15px 0;
            padding: 12px 20px;
            background: var(--gradient-purple);
            border-radius: var(--border-radius);
            color: white;
            font-weight: 600;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            box-shadow: 0 2px 8px rgba(156, 39, 176, 0.2);
        }

        .weight-sum.invalid {
            background: linear-gradient(135deg, #f44336, #e91e63);
            color: white;
        }

        .btn-primary {
            background-color: var(--primary-color);
            border: none;
            padding: 10px 20px;
            border-radius: var(--border-radius);
            font-weight: 600;
            transition: all 0.3s ease;
            margin-top: 10px;
        }

        .btn-primary:hover {
            background-color: #357abd;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(74, 144, 226, 0.2);
        }

        .nav-tabs .nav-link {
            color: #4a5568;
            border: none;
            padding: 10px 20px;
            font-weight: 500;
            border-radius: var(--border-radius) var(--border-radius) 0 0;
        }

        .nav-tabs .nav-link.active {
            color: white;
            background: var(--gradient-purple);
        }

        .tab-content {
            padding: 20px;
            background: white;
            border-radius: 0 0 var(--border-radius) var(--border-radius);
        }

        .job-select {
            margin-bottom: 20px;
        }

        @media (max-width: 768px) {
            body {
                padding: 10px;
            }
            .weight-container {
                margin: 0 10px;
                padding: 15px;
            }
            .weight-group {
                padding: 10px;
                margin-bottom: 10px;
            }
        }
    </style>
</head>
<body>
    <div class="container weight-container">
        <h2 class="page-title">面试评分权重设置</h2>
        
        <ul class="nav nav-tabs" id="weightTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="company-tab" data-bs-toggle="tab" data-bs-target="#company-weights" type="button" role="tab" aria-controls="company-weights" aria-selected="true">公司默认权重</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="job-tab" data-bs-toggle="tab" data-bs-target="#job-weights" type="button" role="tab" aria-controls="job-weights" aria-selected="false">职位特定权重</button>
            </li>
        </ul>
        
        <div class="tab-content" id="weightTabsContent">
            <!-- 公司默认权重设置 -->
            <div class="tab-pane fade show active" id="company-weights" role="tabpanel" aria-labelledby="company-tab">
                <form id="companyWeightForm">
            <div class="weight-group">
                        <label for="technical_weight" class="form-label">专业能力权重</label>
                        <input type="number" class="form-control company-weight-input" id="technical_weight" 
                       step="0.01" min="0" max="1" required>
                <i class='bx bx-code-alt weight-icon'></i>
            </div>
            <div class="weight-group">
                <label for="learning_weight" class="form-label">学习能力权重</label>
                        <input type="number" class="form-control company-weight-input" id="learning_weight" 
                       step="0.01" min="0" max="1" required>
                <i class='bx bx-book weight-icon'></i>
            </div>
            <div class="weight-group">
                <label for="team_weight" class="form-label">团队协作权重</label>
                        <input type="number" class="form-control company-weight-input" id="team_weight" 
                       step="0.01" min="0" max="1" required>
                <i class='bx bx-group weight-icon'></i>
            </div>
            <div class="weight-group">
                <label for="problem_solving_weight" class="form-label">问题解决权重</label>
                        <input type="number" class="form-control company-weight-input" id="problem_solving_weight" 
                       step="0.01" min="0" max="1" required>
                <i class='bx bx-bulb weight-icon'></i>
            </div>
            <div class="weight-group">
                <label for="communication_weight" class="form-label">沟通表达权重</label>
                        <input type="number" class="form-control company-weight-input" id="communication_weight" 
                       step="0.01" min="0" max="1" required>
                <i class='bx bx-conversation weight-icon'></i>
            </div>
                    <div class="weight-sum" id="companyWeightSum">
                <span>权重总和</span>
                        <span id="companySum">0</span>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">保存公司默认设置</button>
                </form>
            </div>
            
            <!-- 职位特定权重设置 -->
            <div class="tab-pane fade" id="job-weights" role="tabpanel" aria-labelledby="job-tab">
                <div class="job-select">
                    <label for="job_selector" class="form-label">选择职位</label>
                    <select class="form-control" id="job_selector"></select>
                </div>
                
                <form id="jobWeightForm">
                    <div id="jobWeightFields">
                        <div class="weight-group">
                            <label for="job_technical_weight" class="form-label">专业能力权重</label>
                            <input type="number" class="form-control job-weight-input" name="技术能力" 
                                   step="0.01" min="0" max="1" required>
                            <i class='bx bx-code-alt weight-icon'></i>
                        </div>
                        <div class="weight-group">
                            <label for="job_learning_weight" class="form-label">学习能力权重</label>
                            <input type="number" class="form-control job-weight-input" name="学习能力" 
                                   step="0.01" min="0" max="1" required>
                            <i class='bx bx-book weight-icon'></i>
                        </div>
                        <div class="weight-group">
                            <label for="job_team_weight" class="form-label">团队协作权重</label>
                            <input type="number" class="form-control job-weight-input" name="团队协作" 
                                   step="0.01" min="0" max="1" required>
                            <i class='bx bx-group weight-icon'></i>
                        </div>
                        <div class="weight-group">
                            <label for="job_problem_solving_weight" class="form-label">问题解决权重</label>
                            <input type="number" class="form-control job-weight-input" name="问题解决" 
                                   step="0.01" min="0" max="1" required>
                            <i class='bx bx-bulb weight-icon'></i>
                        </div>
                        <div class="weight-group">
                            <label for="job_communication_weight" class="form-label">沟通表达权重</label>
                            <input type="number" class="form-control job-weight-input" name="沟通表达" 
                                   step="0.01" min="0" max="1" required>
                            <i class='bx bx-conversation weight-icon'></i>
                        </div>
                    </div>
                    
                    <div class="weight-sum" id="jobWeightSum">
                        <span>权重总和</span>
                        <span id="jobSum">0</span>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">保存职位权重</button>
        </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取当前公司权重设置
            fetch('/api/score-weights')
                .then(response => response.json())
                .then(data => {
                    Object.keys(data).forEach(key => {
                        document.getElementById(key).value = data[key];
                    });
                    updateCompanyWeightSum();
                });
            
            // 获取公司所有职位
            fetch('/api/job/company-jobs')
                .then(response => response.json())
                .then(data => {
                    const jobSelector = document.getElementById('job_selector');
                    jobSelector.innerHTML = '<option value="">请选择职位</option>';
                    
                    if (data.jobs && data.jobs.length > 0) {
                        data.jobs.forEach(job => {
                            const option = document.createElement('option');
                            option.value = job.job_id;
                            option.textContent = job.title;
                            jobSelector.appendChild(option);
                        });
                    }
                });
            
            // 当选择职位时获取该职位的权重设置
            document.getElementById('job_selector').addEventListener('change', function() {
                const jobId = this.value;
                if (!jobId) return;
                
                fetch(`/api/get-weights?job_id=${jobId}`)
                    .then(response => response.json())
                    .then(data => {
                        const inputs = document.querySelectorAll('.job-weight-input');
                        
                        // 如果有权重数据，填充表单
                        if (data.weights && data.weights.length > 0) {
                            inputs.forEach(input => {
                                const weightName = input.getAttribute('name');
                                const weightObj = data.weights.find(w => w.name === weightName);
                                if (weightObj) {
                                    input.value = weightObj.value;
                                } else {
                                    input.value = 0.2; // 默认值
                                }
                            });
                        } else {
                            // 如果没有设置过的权重，使用默认值
                            inputs.forEach(input => {
                                input.value = 0.2;
                            });
                        }
                        
                        updateJobWeightSum();
                    });
                });

            // 监听公司权重输入变化，更新总和
            document.querySelectorAll('.company-weight-input').forEach(input => {
                input.addEventListener('input', updateCompanyWeightSum);
            });
            
            // 监听职位权重输入变化，更新总和
            document.querySelectorAll('.job-weight-input').forEach(input => {
                input.addEventListener('input', updateJobWeightSum);
            });

            // 提交公司权重表单
            document.getElementById('companyWeightForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const weightSum = calculateCompanyWeightSum();
                if (Math.abs(weightSum - 1) > 0.001) {
                    alert('所有权重之和必须等于1');
                    return;
                }

                const data = {};
                document.querySelectorAll('.company-weight-input').forEach(input => {
                    data[input.id] = parseFloat(input.value);
                });

                fetch('/api/score-weights', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(data)
                })
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        alert(data.error);
                    } else {
                        alert('公司默认权重更新成功！');
                    }
                });
            });

            // 提交职位权重表单
            document.getElementById('jobWeightForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const jobId = document.getElementById('job_selector').value;
                if (!jobId) {
                    alert('请先选择一个职位');
                    return;
                }
                
                const weightSum = calculateJobWeightSum();
                if (Math.abs(weightSum - 1) > 0.001) {
                    alert('所有权重之和必须等于1');
                    return;
                }
                
                const weights = [];
                document.querySelectorAll('.job-weight-input').forEach(input => {
                    weights.push({
                        name: input.getAttribute('name'),
                        value: parseFloat(input.value)
                    });
                });
                
                fetch('/api/save-weights', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        job_id: jobId,
                        weights: weights
                    })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.error) {
                        alert(data.error);
                    } else {
                        alert('职位权重保存成功！');
                    }
                });
            });

            function updateCompanyWeightSum() {
                const sum = calculateCompanyWeightSum();
                const sumElement = document.getElementById('companySum');
                sumElement.textContent = sum.toFixed(2);
                
                const weightSumDiv = document.getElementById('companyWeightSum');
                if (Math.abs(sum - 1) > 0.001) {
                    weightSumDiv.classList.add('invalid');
                } else {
                    weightSumDiv.classList.remove('invalid');
                }
            }

            function updateJobWeightSum() {
                const sum = calculateJobWeightSum();
                const sumElement = document.getElementById('jobSum');
                sumElement.textContent = sum.toFixed(2);
                
                const weightSumDiv = document.getElementById('jobWeightSum');
                if (Math.abs(sum - 1) > 0.001) {
                    weightSumDiv.classList.add('invalid');
                } else {
                    weightSumDiv.classList.remove('invalid');
                }
            }

            function calculateCompanyWeightSum() {
                let sum = 0;
                document.querySelectorAll('.company-weight-input').forEach(input => {
                    sum += parseFloat(input.value) || 0;
                });
                return sum;
            }
            
            function calculateJobWeightSum() {
                let sum = 0;
                document.querySelectorAll('.job-weight-input').forEach(input => {
                    sum += parseFloat(input.value) || 0;
                });
                return sum;
            }
        });
    </script>
</body>
</html> 